package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.CustomerManagement;
import com.woniuxy.entity.dto.CustomerManagementDTO;
import com.woniuxy.entity.dto.FapiaoDTO;
import com.woniuxy.mapper.FapiaoMapper;
import com.woniuxy.service.FapiaoService;
import com.woniuxy.util.PageData;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.SnowflakeIdGenerator;
import com.woniuxy.util.UserContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: tsm
 * @description:
 * @create: 2024-10-25 15:39
 **/
@Service
public class FapiaoServiceImpl implements FapiaoService {
    @Resource
    private FapiaoMapper fapiaoMapper;
    @Override
    public ResponseData<?> queryFapiao(PageData<FapiaoDTO> pageData) {
        PageHelper.startPage(pageData.getPageNum(),pageData.getPageSize());
        List<FapiaoDTO> fapiaoDTOS = fapiaoMapper.queryFapiao(pageData.getParam());
        PageInfo pageInfo = new PageInfo(fapiaoDTOS);
        return new ResponseData<>().ok(pageInfo);
    }

    @Override
    public ResponseData<?> add(FapiaoDTO fapiaoDTO) {
        CustomerManagement customerManagement = new CustomerManagement();
        customerManagement.setName(fapiaoDTO.getCustomerId());
        customerManagement.setCompanyContact(fapiaoDTO.getTel());
        fapiaoMapper.addByCustom(customerManagement);

        Long userId = UserContext.getUserId();
        fapiaoDTO.setRId(String.valueOf(userId));

        SnowflakeIdGenerator sg = new SnowflakeIdGenerator();
        long l = sg.nextId();
        fapiaoDTO.setId(String.valueOf(l));

        fapiaoMapper.add(fapiaoDTO);

        return new ResponseData<>().ok();
    }
}
